<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h2 class="title">客人信息：
	<a href="javascript:;" onclick="oldcustomer();">老客户</a>
	<a href="javascript:;" onclick="newcustomer();">新客户</a>
</h2>

<div class="b_div" style="display:none;">
	<div id="final_customer_table">
		<input type="hidden" name="id" value="${buyback_customer.id }">
		<p class="clearfix">
			<span class="info_s fl">客户名称：<span name="name">${buyback_customer.name }</span></span>
			<span class="info_s fl">联系电话：<span name="tel">${buyback_customer.tel }</span></span>
			<span class="info_s fl">传真：<span name="fax">${buyback_customer.fax }</span></span>
			<span class="info_s fl">邮箱：<span name="email">${buyback_customer.email }</span></span>
		</p>
		<p class="clearfix">
			<span class="info_s fl">公司地址：<span name="comaddress">${buyback_customer.comaddress }</span></span>
		</p>
		<p class="clearfix">
			<span class="info_s fl">送货地址：<span name="address">${buyback_customer.address }</span></span>
		</p>
		<p class="clearfix">
			<span class="info_s fl">银行账号：<span name="bank_no">${buyback_customer.bank_no }</span></span>
			<span class="info_s fl">户名：<span name="account_name">${buyback_customer.account_name }</span></span>
			<span class="info_s fl">开户行：<span name="bank_name">${buyback_customer.bank_name }</span></span>
		</p>
	</div>
</div>

<div id="oldcustomer_dialog" style="font-size:10px;">
	<form id="oldcustomer_form">
	姓名：<input type="text" name="name">&nbsp;手机号：<input type="text" name="tel">
	<input type="button" value="查找" onclick="findcustomer();">
	<input type="button" value="确定" onclick='oldconfirm();'></form><hr>
	<form>
	<table id="oldcustomer_table" style="width:100%">
		
	</table>
	</form>
</div>

<div id="newcustomer_dialog" style="font-size:10px;">
	<form id="newcustomer_form">
	<table id="newcustomer_table">
		<tr>
			<th>客户类别：</th>
			<td>
				<select name="sort">
				<option value="">--请选择--</option>
				<c:forEach items="${customertypes }" var="type">
					<option value="${type.code }">${type.desc }</option>
				</c:forEach>	
				</select>
			</td>
		</tr>
		<tr><th>客户姓名：</th><td><input type="text" name="name"></td></tr>
		<tr><th>联系电话：</th><td><input type="text" name="telphone"></td></tr>
		<tr><th>传真：</th><td><input type="text" name="fax"></td></tr>
		<tr><th>邮箱：</th><td><input type="text" name="email"></td></tr>
		<tr><th>公司地址：</th><td><input type="text" name="comaddress"></td></tr>
		<tr><th>送货地址：</th><td><input type="text" name="address"></td></tr>
		<tr><th>银行账号：</th><td><input type="text" name="bank_no"></td></tr>
		<tr><th>户名：</th><td><input type="text" name="account_name"></td></tr>
		<tr><th>开户行：</th><td><input type="text" name="bank_name"></td></tr>
		<input type="hidden" name="id">
	</table>
	</form>
</div>

<script type="text/javascript">
	function oldcustomer() {
		$("#oldcustomer_dialog").dialog("open");
	}
	
	function newcustomer() {
		$("#newcustomer_dialog").dialog("open");
	}
	
	function afteradd() {
		$.ajax({
			url:"${basePath}/scat/addCustomer.htm",
			type:"post",
			data:$("#newcustomer_form").serialize(),
			dataType:"json",
			success:function(data) {
				$.jqalert(data.msg, "提示", function(){
					if(data.isSuccess) {
						$("#newcustomer_dialog").dialog("close");
						var id = data.json.id;
						$("#newcustomer_table").find("input[name='id']").val(id);
						newconfirm();
						$("#newcustomer_form").find("input").val("");
					}
				});
			}
		});
	}
	
	//查询老客户
	function findcustomer() {
		var name = $("#oldcustomer_form").find("input[name='name']").val();
		var tel = $("#oldcustomer_form").find("input[name='tel']").val();
		if(name==""&&tel=="") $.jqalert("请输入查询条件","提示");
		else {
			$("#oldcustomer_table").load("${basePath}/customer/loadUsers.htm?name="
					+name+"&tel="+tel);
		}
	}
	
	function oldconfirm() {
		//$("#final_customer_table")
		var radio = $("input[name='customer_radio']:checked");
		var customerId = radio.val();
		//alert(customerId);
		$("#final_customer_table").find("input[name='id']").val(customerId);
		$.each(radio.parent().parent().find("input[type='hidden']"), function(i,n){
			$("#final_customer_table").find("span[name='"+$(n).attr("name")+"']")
				.text($(n).val());
		});
		$("#oldcustomer_dialog").dialog("close");
		$(".b_div").show();
		$("#oldcustomer_table").html("");
		$("#oldcustomer_form").find("input").val("");
	}
	
	function newconfirm() {
		$("#final_customer_table").find("input[name='id']").val(
			$("#newcustomer_table").find("input[name='id']").val());
		$.each($("#newcustomer_table").find("input[type='text']"), function(i,n){
			$("#final_customer_table").find("span[name='"+$(n).attr("name")+"']")
			.text($(n).val());
		});
		$(".b_div").show();
	}
	
	$(function() {
		$("#oldcustomer_dialog").dialog({
			title:"客户信息",
			autoOpen: false,
			height: 300,
			width: 500,
			modal: true,
		});
		
		$("#newcustomer_dialog").dialog({
			title:"添加新客户",
			autoOpen: false,
			height: 400,
			width: 300,
			modal: true,
			buttons:[{
				text:"确定",
				click:afteradd
			},{
				text:"清空",
				click:function() {
					$("#newcustomer_form").find("input").val("");
				}
			}]
		});
		
		<c:if test="${buyback_customer ne null}">
		$(".b_div").show();
		</c:if>
	});
</script>